package com.clarence.dada.core.redis.start;


import com.clarence.dada.core.redis.RedisOperatorApi;
import com.clarence.dada.core.redis.operator.type.RoleRedisCache;
import com.clarence.dada.core.redis.operator.type.RoleResourceRedisCache;
import com.clarence.dada.core.redis.operator.type.SysUserRedisCache;
import com.clarence.dada.core.redis.operator.type.UserRoleRedisCache;
import com.clarence.dada.core.redis.util.CreateRedisTemplateUtil;
import com.clarence.dada.modular.system.role.entity.SysRoleEntity;
import com.clarence.dada.modular.system.user.entity.resp.SysUserResp;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * 系统管理缓存的自动配置，Redis配置
 *
 * @author fengshuonan
 * @since 2022/11/8 23:26
 */
@Configuration
@ConditionalOnClass(name = "org.springframework.data.redis.connection.RedisConnectionFactory")
public class SystemRedisCacheAutoConfiguration {

    /**
     * 用户的缓存，非在线用户缓存，此缓存为了加快查看用户相关操作
     *
     * @author fengshuonan
     * @since 2022/11/8 23:32
     */
    @Bean
    public RedisOperatorApi<SysUserResp> sysUserRedisOperatorApi(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SysUserResp> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
        return new SysUserRedisCache(redisTemplate);
    }

    /**
     * 用户角色对应的缓存
     *
     * @author fengshuonan
     * @since 2022/11/8 23:32
     */
    @Bean
    public RedisOperatorApi<List<Long>> userRoleCacheApi(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, List<Long>> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
        return new UserRoleRedisCache(redisTemplate);
    }

    /**
     * 角色信息对应的缓存
     *
     * @author fengshuonan
     * @since 2022/11/8 23:32
     */
    @Bean
    public RedisOperatorApi<SysRoleEntity> roleInfoCacheApi(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, SysRoleEntity> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
        return new RoleRedisCache(redisTemplate);
    }

    /**
     * 用户资源绑定的缓存
     *
     * @author fengshuonan
     * @since 2022/11/8 23:32
     */
    @Bean
    public RedisOperatorApi<List<String>> roleResourceCacheApi(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, List<String>> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
        return new RoleResourceRedisCache(redisTemplate);
    }

//    /**
//     * 接口统计的缓存
//     *
//     * @author fengshuonan
//     * @since 2022/11/8 23:32
//     */
//    @Bean
//    public RedisOperatorApi<Map<Long, Integer>> requestCountCacheApi(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Map<Long, Integer>> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
//        return new InterfaceStatisticsRedisCache(redisTemplate);
//    }

}
